import React, {PureComponent} from "react";
import {connect} from "react-redux";
import {Form} from "antd";
import {query} from "./actions";
import {DATE_FORMAT} from "../../lib/func";
import FilterForm from "../../components/FilterForm";
import {getCurrentOrganization, getLastFilter, getPrincipal, saveLastFilter} from "../../lib/identity";
import {getClientList} from "../home/actions";
const LAST_FILTER = "containerOrder";

class Filter extends PureComponent {
    state = {};
    handleSearch = (values) => {
        const {dispatch} = this.props;
        let fields = values;
        this.setState({...fields});
        if (fields.createTimeRange && fields.createTimeRange.length > 1) {
            fields.createTimeStart = fields.createTimeRange[0].format(DATE_FORMAT);
            fields.createTimeEnd = fields.createTimeRange[1].format(DATE_FORMAT);
        }
        fields.userId = getPrincipal().id;
        delete fields.createTimeRange;
        saveLastFilter(LAST_FILTER, {...fields});
        dispatch(query({...fields, "organizationId": getCurrentOrganization()}));
    };

    changeOrg = value => {
        const {dispatch} = this.props;
        this.setState({"organizationId": value});
        dispatch(getClientList(value));
    };

    render() {
        const {loading, clientList, organizations} = this.props;
        let fields = getLastFilter(LAST_FILTER);
        if (fields === null || fields === undefined) {
            fields = {};
        }
        const filterSchema = [
            {
                key: 'businessNumber',
                field: 'businessNumber',
                type: 'text',
                expandable: true,
                width: 200,
                title: '业务编号',
                fieldOptions: {
                    initialValue: this.state.businessNumber || fields.businessNumber
                }
            }, {
                key: 'clientId',
                field: 'clientId',
                type: 'listSelector',
                expandable: true,
                title: '客户',
                fieldOptions: {
                    initialValue: this.state.clientId || fields.clientId
                },
                controlProps: {
                    dataSource: clientList,
                    labelField: "name",
                    valueField: "id"
                }
            }, {
                key: 'cabinetNumber',
                field: 'cabinetNumber',
                type: 'text',
                expandable: true,
                width: 200,
                title: '柜号',
                fieldOptions: {
                    initialValue: this.state.cabinetNumber || fields.cabinetNumber
                }
            }, {
                key: 'createTimeRange',
                field: 'createTimeRange',
                type: 'dateRangePicker',
                expandable: true,
                title: '创建日期',
                fieldOptions: {
                    initialValue: this.state.createTimeRange || fields.createTimeRange
                }
            }
        ];
        filterSchema.splice(0, 0, {
            key: 'organizationId',
            field: 'organizationId',
            type: 'treeSelector',
            expandable: true,
            width: 200,
            title: '所属机构',
            controlProps: {
                treeData: organizations,
                labelField: "name",
                valueField: "id",
                treeCheckable: false,
                onChange: this.changeOrg
            },
            fieldOptions: {
                initialValue: this.state.organizationId
            }
        });
        return (
            <FilterForm schema={filterSchema} callback={this.handleSearch} loading={loading}/>
        );
    }
}

const mapStateToProps = state => {
    return {
        ...state.containerOrder.list,
        clientList: state.home.clients,
        packageReturnType: state.common.packageReturnType,
        organizations: state.home.organizations
    };
};

export default connect(mapStateToProps)(Form.create()(Filter));
